<?php
class Invite extends  Controller
{
    function Invite()
    {
        parent::Controller();
      
		header("Content-Type: text/html; charset=utf-8");
		
    }
    
    
    function index()
    {
    	
       
    	if($this->session->userdata('invite')=="" && !config_item("open_register"))
        {
        	output($this,"系统需要邀请注册,请发邮件至zero.yang@tipcat.com索取!");
        }else
        {
        	$query = $this->db->get("area");
        	$rs = $query->result_array();
        	$area = array();
        	foreach ($rs as $v)
        	{
        		$area[$v["areaid"]] = $v["area"];
        	}
        	$this->tp->assign("area",$area);
        	$this->tp->show("invite");
        }
       
    	
    }
    function getcode($code="")
    {
    	
		if(!empty($code))
		{
			$query = $this->db->get_where("invite",array("code"=>$code,"state"=>0));
			if($query->num_rows()>0)
			{
				$rs = $query->row_array();
				$this->session->set_userdata(array('invite'=>$rs["inviteid"]));
				$this->db->update("invite",array("state"=>1),array("inviteid"=>$rs["inviteid"]));
				js(templateurl."invite");
			}
			
		}
    }
    function save()
    {
    	if($this->session->userdata('invite')=="" && !config_item("open_register"))
        {
        	output($this,"系统需要邀请注册,请发邮件至zero.yang@tipcat.com索取!");
        }else
        {
        	$data = array(
        			'inviteid'=>config_item("open_register")?0:$this->session->userdata('invite'),
					'username'=>$_POST["email"],
					'password'=>md5($_POST["password"]),
					'nickname'=>$_POST["nickname"],
					'areaid'=>$_POST["area"],
					'companyid'=>$_POST["company"],
					'state'=>0,
					'islock'=>0
        	
        	);
        	$this->db->insert("users",$data);
        	$this->session->unset_userdata(array("invite"=>""));
        	if($this->db->insert_id()>0)
        	{
        		$query2 = $this->db->query("select endtime from ".table("company")." where companyid=".$data["companyid"]);
        		$rs = $query2->row_array();
        		$data = array(
    						"userid"=>$this->db->insert_id(),
    						"companyid"=>$data["companyid"],
    						"areaid"=>$data["areaid"],
    						"cartend"=>$rs["endtime"]
    			           );
    			$this->session->set_userdata("memberdata",$data);
        	}
        	js(templateurl."orders");
        }
    }
    
    
    
}    
?>
